package com.example.echoewish.ui.my_activity.my_publish_activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.echoewish.pojo.MyPublishPost;
import com.example.echoewish.R;

import java.util.List;

//采用监听器模式
//所以 可以认为 该适配器类并不知道activity的存在。
public class MyPublishPostAdapter extends ArrayAdapter<MyPublishPost> {

    private Context activity;
    private  int resource;
    private  ButtonClickListener buttonClickListener;

    public MyPublishPostAdapter(@NonNull Context context, int resource, @NonNull List<MyPublishPost> objects) {
        super(context, resource, objects);
        activity=context;
        this.resource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view= LayoutInflater.from(activity).inflate(resource,parent,false);
        MyPublishPost myPublishPost=getItem(position);
        TextView titleTextView=view.findViewById(R.id.my_publish_title_text_view);
        TextView contentTextview=view.findViewById(R.id.my_publish_content_text_view);
        TextView zoneTextView=view.findViewById(R.id.my_publish_zone_text_view);
        TextView timeTextView=view.findViewById(R.id.my_publish_time_text_view);
        Button deleteButton=view.findViewById(R.id.my_publish_delete_button);

        if(myPublishPost!=null)
        {
            titleTextView.setText(myPublishPost.title);
            contentTextview.setText(myPublishPost.content);
            timeTextView.setText(myPublishPost.time);
            zoneTextView.setText(myPublishPost.zone);
        }

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClickListener.buttonClickListener(position);
            }

        });
        return view;
    }

    public void setButtonClickListener(ButtonClickListener buttonClickListener){
        this.buttonClickListener=buttonClickListener;
    }

    public interface ButtonClickListener
    {
        void buttonClickListener(int nowPosition);
    }
}
